package com.renyi.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.MybatisService;
import com.renyi.model.MybatisServiceDoctor;
import com.renyi.model.Question;
import com.renyi.model.QuestionExtra;
import com.renyi.util.CalendarUtil;
import com.renyi.util.Constants;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class DoctorController extends BaseController {

	@Autowired
	private MybatisServiceDoctor mybatisServiceDoctor;

	// 获取快速提问列表
	@RequestMapping("getQuestionList")
	public void getQuestionList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String departmentID = paramMap.get("departmentID") != null ? paramMap.get("departmentID").toString() : "1";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			ArrayList<Question> listData = new ArrayList<Question>();
			Question question;
			Iterator iter = Constants.questionkHm.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				Object key = entry.getKey();
				QuestionExtra questionExtra = (QuestionExtra) entry.getValue();
				if (questionExtra != null && questionExtra.getIsDeal() == 0
						&& questionExtra.getDepartmentID().equals(departmentID)) {
					question = new Question();
					question.setChatQQ(questionExtra.getChatQQ());
					question.setId(questionExtra.getId());
					question.setQuestionDes(questionExtra.getQuestionDes());
					question.setCreateTime(questionExtra.getCreateTime());
					question.setTelephone(questionExtra.getTelephone());
					listData.add(question);
				}
			}
			result.setCode(0); // 0：成功
			result.setData(listData);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-抢答问题
	@RequestMapping("dealQuestion")
	public void dealQuestion(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String id = paramMap.get("id") != null ? paramMap.get("id").toString() : "";
			if ("".equals(id)) {
				result.setCode(-1); // 0：成功
				result.setMsg("问题编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			QuestionExtra questionExtra = (QuestionExtra) Constants.questionkHm.get(id);
			if (questionExtra != null) {
				if (questionExtra.getIsDeal() == 1) {
					result.setCode(1); // 0：成功 1:被其它好心医生抢答
					result.setMsg("该问题已经被好心医生抢答");
				} else {
					questionExtra.setIsDeal(1);
					questionExtra.setDoctorID(uid);
					String curTime = CalendarUtil.getCurrentDateTime();
					questionExtra.setDealTime(curTime);
					result.setCode(0);
				}

			} else {
				result.setCode(-1); // 0：成功
				result.setMsg("该问题不存在");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-获取我的诊所信息
	@RequestMapping("getClinicInfo")
	public void getClinicInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("医生编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);

			List<HashMap<String, Object>> list = this.mybatisServiceDoctor.findMybatisList("getClinicInfoSQL", pMap);
			HashMap<String, Object> hs = new HashMap<String, Object>();
			if (list!=null && list.size() > 0) {
				hs = list.get(0);
				result.setData(hs);
			}
			result.setCode(0); // 0：成功
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-服务价格设置
	@RequestMapping("/setServiceFee")
	public void setServiceFee(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();

			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String imageTextFee = paramMap.get("imageTextFee") != null ? paramMap.get("imageTextFee").toString() : "0";
			String iTSwitch = paramMap.get("iTSwitch") != null ? paramMap.get("iTSwitch").toString() : "0";
			String telephoneFee = paramMap.get("telephoneFee") != null ? paramMap.get("telephoneFee").toString() : "0";
			String tHSwitch = paramMap.get("tHSwitch") != null ? paramMap.get("tHSwitch").toString() : "0";
			String type = paramMap.get("type") != null ? paramMap.get("type").toString() : "";

			if ("".equals(type)) {
				result.setCode(-1); // 0：成功
				result.setMsg("操作类型参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("imageTextFee", imageTextFee);
			pMap.put("iTSwitch", iTSwitch);
			pMap.put("telephoneFee", telephoneFee);
			pMap.put("tHSwitch", tHSwitch);

			if (type.equals("1")) {
				int iKey = (Integer) this.mybatisServiceDoctor.insertIbatisObject("insertServiceFeeSQL", pMap);
				if (iKey > 0) {
					result.setCode(0); // 0：成功
					result.setMsg("保存成功");
				} else {
					result.setCode(-1); // 0：成功
					result.setMsg("保存失败");
				}
			} else if (type.equals("2")) {
				int uKey = this.mybatisServiceDoctor.updateIbatisObject("updateServiceFeeSQL", pMap);
				if (uKey > 0) {
					result.setCode(0); // 0：成功
					result.setMsg("保存成功");
				} else {
					result.setCode(-1); // 0：成功
					result.setMsg("保存失败");
				}
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-关闭当前对话
	@RequestMapping("closeRecord")
	public void closeRecord(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String doctorQQ = paramMap.get("doctorQQ") != null ? paramMap.get("doctorQQ").toString() : "";
			String patientQQ = paramMap.get("patientQQ") != null ? paramMap.get("patientQQ").toString() : "";

			if ("".equals(uid) || "".equals(doctorQQ) || "".equals(patientQQ)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("doctorQQ", doctorQQ);
			pMap.put("patientQQ", patientQQ);

			int key = this.mybatisServiceDoctor.updateIbatisObject("closeRecordSQL", pMap);
			if (key > 0) {
				result.setCode(0); // 0：成功
			} else {
				result.setCode(-1); // 0：成功
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 医生-查看当前对话是否关闭
	@RequestMapping("getDoctorRecordState")
	public void getDoctorRecordState(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String doctorQQ = paramMap.get("doctorQQ") != null ? paramMap.get("doctorQQ").toString() : "";
			String patientQQ = paramMap.get("patientQQ") != null ? paramMap.get("patientQQ").toString() : "";

			if ("".equals(doctorQQ)) {
				result.setCode(-1); // 0：成功
				result.setMsg("医生QQ参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("doctorQQ", doctorQQ);
			pMap.put("patientQQ", patientQQ);
			HashMap<String, Object> rMap = new HashMap<String, Object>();
			List<HashMap<String, Object>> list = this.mybatisServiceDoctor.findMybatisList("getDoctorRecordStateSQL",
					pMap);
			if (list != null && list.size() > 0) {
				result.setData(list.get(0));
			} else {
				list = this.mybatisServiceDoctor.findMybatisList("getDoctorStateSQL",
						pMap);
				if (list != null && list.size() > 0) {
					result.setData(list.get(0));
				}
			}
			result.setCode(0); // 0：成功
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

}
